#!/usr/bin/env python
"""
PyLink-SR (Python Linked Software Radio System)
(c) Bob Cowdery (G3UKB) 2008

File: streams.py
Date: July 2008

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

The author can be reached by email at

bob@g3ukb.co.uk

========================================================================
Module Description:

Manage the Jack streams

"""

#-----------------------------------------------------------------------
# System imports
import logging
import sys
from time import sleep
from scipy import *
import Numeric
import jack

# Application imports
import config_service as config
import logger_service as logger
import dsp

#-----------------------------------------------------------------------
def init():    
    global capture
    global real_out_as_numeric
    global logger

    # Get us a logger
    logger = logging.getLogger('pylink.logger')

    # Initialise the input and output arrays
    FRAMES = config.frames()
    capture = Numeric.zeros((2,FRAMES), 'f')
    real_out_as_numeric = Numeric.zeros((2,FRAMES), 'f')

#-------------------------------------------------------------------
def do_pass(dsp_impl):
    global N
    global capture
    global real_out_as_numeric

    try:
        # Exchange the streams
        jack.process(real_out_as_numeric, capture)
    except jack.InputSyncError:
        logger.info("Jack - Input Sync Error") 
        pass
    except jack.OutputSyncError:
        logger.info("Jack - Output Sync Error") 
        pass
    
    # The DSP wants doubles
    capture_as_double = capture.astype('d')
    # Execute one pass of the DSP
    real_out = dsp_impl.do_rx(capture_as_double)

    # Translate the stream as pyjack uses Numeric float arrays    
    real_out_as_float = real_out.astype('f')
    real_out_as_numeric = Numeric.asarray(real_out_as_float)
        

